package com.example.recyclelibrary;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.graphics.Canvas;
import android.graphics.Paint;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eddy on 5/5/17.
 * 自定义圆loading
 */

public class CircleProgress extends BaseProgressController {

    public static final float SCALE=1.0f;//缩放倍数

    public static final int ALPHA=255;//透明度
    float[] scaleFloats=new float[]{SCALE,SCALE,SCALE,SCALE,SCALE,SCALE,SCALE,SCALE};
    int[] alphas=new int[]{ALPHA,ALPHA,ALPHA,ALPHA,ALPHA,ALPHA,ALPHA,ALPHA};

    @Override
    public void draw(Canvas canvas, Paint paint) {
        float radius=getWidth()/10;
        for(int i=0;i<8;i++){
            canvas.save();
            Point point=circleAt(getWidth(),getHeigth(),getWidth()/2-radius,i*(Math.PI/4));
            canvas.translate(point.x,point.y);
            canvas.scale(scaleFloats[i],scaleFloats[i]);
            paint.setAlpha(alphas[i]);
            canvas.drawCircle(0,0,radius,paint);
            canvas.restore();
        }
    }

    /**
     *计算圆点的位置
     * @param width
     * @param height
     * @param radius
     * @param angle
     * @return
     */
    Point circleAt(int width,int height,float radius,double angle){
        float x=(float) (width/2+radius*(Math.cos(angle)));
        float y= (float) (height/2+radius*(Math.sin(angle)));
        return new Point(x,y);
    }

//    创建圆点的属性动画
    @Override
    public List<Animator> createAnimation() {
        final List<Animator> animators=new ArrayList<>();
        int[] delays={0,120,240,360,480,600,720,780,840};
        for (int i=0;i<8;i++){
            final int index=i;
            ValueAnimator scaleAnim=ValueAnimator.ofFloat(1,0.4f,1);
            scaleAnim.setDuration(1000);
            scaleAnim.setRepeatCount(-1);
            scaleAnim.setStartDelay(delays[i]);
            scaleAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    scaleFloats[index]= (float) valueAnimator.getAnimatedValue();
                    postInvalidate();
                }
            });
            scaleAnim.start();
            final ValueAnimator alphaAnim=ValueAnimator.ofInt(255,77,255);
            alphaAnim.setDuration(1000);
            alphaAnim.setRepeatCount(-1);
            alphaAnim.setStartDelay(delays[i]);
            alphaAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator valueAnimator) {
                    alphas[index]= (int) valueAnimator.getAnimatedValue();
                    postInvalidate();
                }
            });
            alphaAnim.start();
            animators.add(scaleAnim);
            animators.add(alphaAnim);
        }
        return animators;
    }

//    点的对象
    final class Point{
        public float x;
        public float y;

        public Point(float x, float y){
            this.x=x;
            this.y=y;
        }
    }

}
